﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TIS.Imaging;

namespace CaptureImageMergeSys
{
    public partial class InputVideoWnd : Form
    {
        private string m_strDeviceName;

        public InputVideoWnd( string strDeviceName )
        {
            InitializeComponent();
            
            this.TopLevel = false;
            m_strDeviceName = strDeviceName;
        }

        private void btDeviceSelect_Click(object sender, EventArgs e)
        {
            if (icImagingControl.DeviceValid)
            {
                icImagingControl.LiveStop();
            }

            icImagingControl.ShowDeviceSettingsDialog();

            if (icImagingControl.DeviceValid)
            {
                MakeDeviceSettings(icImagingControl);
                // Save the currently used device into a file in order to be able to open it
                // automatically at the next program start.
                icImagingControl.SaveDeviceStateToFile( strDeviceName + ".xml");
                //icImagingControl1.LiveStart();
                // tell Application Controller that Device is ready
                ApplicationController.Instance().SetDeviceStatus(m_strDeviceName, true);
            }
        }

        /// <summary>
        /// MakeDeviceSettings
        ///
        /// Setup the ring buffer, color format and capture mode.
        /// </summary>
        private void MakeDeviceSettings(TIS.Imaging.ICImagingControl icImagingControl)
        {
            // Set the color format to monochrome
            icImagingControl.MemoryCurrentGrabberColorformat = TIS.Imaging.ICImagingControlColorformats.ICRGB32;

            // Set the ring buffer size to 5. This ensures that the last 5
            // acquired images are in memory.
            icImagingControl.ImageRingBufferSize = 5;

            // LiveCaptureContinuous = True means that every frame is
            // copied to the ring buffer.
            icImagingControl.LiveCaptureContinuous = true;

            // Do not save the last image, if liveStop is called
            icImagingControl.LiveCaptureLastImage = false;

            // Disable the live display. This allows to display images
            // from the ring buffer in ICImagingControl//s control window.
            icImagingControl.LiveDisplay = true;

            // Fit image size to window size
            // Adjust live display size to the control size.
            icImagingControl.LiveDisplayDefault = false;
            icImagingControl.LiveDisplayHeight = icImagingControl.Height;
            icImagingControl.LiveDisplayWidth = icImagingControl.Width;

        }

        /// <summary>
        /// icImagingControl_ImageAvailable
        ///
        /// Event is called for each frame.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void icImagingControl_ImageAvailable(object sender, ICImagingControl.ImageAvailableEventArgs e)
        {

            try
            {

            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message);
            }
        }


        /// <summary>
        /// Stop the live video stream, if the application is closed.
        /// </summary>
        private void InputVideoWnd_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (icImagingControl.DeviceValid)
            {
                icImagingControl.LiveStop();
            }
        }

        public bool StartVideo()
        {
            if (icImagingControl.DeviceValid)
            {
                icImagingControl.LiveStart();
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool StopVideo()
        {
            if (icImagingControl.DeviceValid && icImagingControl.LiveVideoRunning)
            {
                icImagingControl.LiveStop();
            }
            return true;
        }

    }
}
